package com.changli03.buffered;

import org.junit.Test;

import java.io.*;

public class CopyTest {

    @Test
    public void CopyTest (){
        int start = (int) System.currentTimeMillis();
        String src = "D:\\后端\\File_ioStream\\src.mp4";
        String dest = "D:\\后端\\File_ioStream\\dest`.mp4";
//        FileStreamCopy(src, dest);//花费的时间为:33094
        BufferedStreamCopy(src, dest);//836



        int end = (int) System.currentTimeMillis();
        System.out.println("花费的时间为:"+ (end-start));
    }





    public void FileStreamCopy(String src,String dest){
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dest);

            byte[] buffer = new byte[50];
            int len = 0;
            while ((len = fis.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(fos!=null)
                    fis.close();

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if(fis!=null)
                    fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }



    public  void BufferedStreamCopy(String src,String dest){
        FileInputStream fis = null;
        FileOutputStream fos = null;

        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dest);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            byte[] buffer = new byte[50];
            int len = 0;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(bos != null) bos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if(bis != null) bis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
